// src/main/java/com/fjnu/dao/impl/TagDaoImpl.java
package com.fjnu.dao.impl;

import com.fjnu.dao.TagDao;
import com.fjnu.entity.Tag;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;

public class TagDaoImpl implements TagDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public int insert(Tag tag) {
        String sql = "INSERT INTO tb_tag(name, create_time, update_time) VALUES(?, ?, ?)";
        return jdbcTemplate.update(sql, tag.getName(), new Date(), new Date());
    }

    @Override
    public int insertArticleTag(Integer articleId, Integer tagId) {
        String sql = "INSERT INTO tb_article_tag(article_id, tag_id) VALUES(?, ?)";
        return jdbcTemplate.update(sql, articleId, tagId);
    }

    @Override
    public List<Tag> selectByName(String name) {
        String sql = "SELECT * FROM tb_tag WHERE name LIKE ?";
        try {
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Tag.class), "%" + name + "%");
        } catch (Exception e) {
            return null;
        }
    }
}